/**
 * All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
 *
 *PmMilepostController
 * 项目里程碑
 *
 * 修改纪录
 * 2020-05-08 版本：1.0 shenyouyi 创建。
 * @version 版本：1.0
 * @author 作者：shenyouyi
 * 创建日期：2020-05-08
 */

package com.topscomm.pm.controller;

import com.topscomm.pm.controller.auto.PmMilepostControllerAuto;
import com.topscomm.pm.pojo.PmMilepostEntity;
import com.topscomm.pm.service.IPmTaskService;
import com.topscomm.pm.utils.MisStringUtil;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.tap.common.TapSystemConst;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * @author: shenyouyi
 * @date: 2020-05-08
 * @description:项目里程碑Controller
 * @modify:
 */
@RestController
@RequestMapping("pm/milepost")
public class PmMilepostController extends PmMilepostControllerAuto implements IPmMilepostController {

	@Autowired
	private IPmTaskService pmTaskService;

	/**
	 * @author: shenyouyi
	 * @date: 2020-05-08
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_projectid")) {
			String value = paraMap.get("qry_projectid").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and projectid = {0} ", value);
		}
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and milepostname like '%{0}%' ", value);
		}

		//新增项目任务时，里程碑选择参照时按照项目名称进行模糊查询
		if (paraMap.containsKey("qry_projectname")) {
			String value = paraMap.get("qry_projectname").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and exists (select id from pmproject where pmproject.id = pmmilepost.projectid and pmproject.name like '%{0}%')", value);
		}

		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_mileState")) {
			String value = paraMap.get("qry_mileState").toString();
			if ("0".equals(value))
				sql.appendSingle(" and isfinished is null");//未达成的里程碑
		}
		sql .appendSingle(this.spellingSqlForProject(paraMap, "PmMilepost","projectid","id"));
		if (this.getUserBean().getUser().getAdminlevel() < 1)// 管理员、超级管理员，不控制权限
		{
			String dataPession = service.getDataPermission(TapSystemConst.TapOperationTypeEnum.Select, "", this.getUserBean());
			if (StringUtil.isEmpty(dataPession)) {
				sql.appendSingle(" and " + MisStringUtil.createWhereByUsercodeFromProject(this.getUserBean().getUser(), "projectid", service.getDao().getTableName()));
			} else// 使用数据权限
			{
				sql.appendSingle(" and {0} ", dataPession);
			}
		}
		return sql.toString();
	}

	/**
	 * @author: shenyouyi
	 * @date: 2020-05-08
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 * 完成
	 * @param paraMap
	 * @return
	 */
	@PostMapping(value = "/finish")
	public ResponseResult<Object> finish(@RequestBody Map<String, Object> paraMap) {
		// 根据projectid查询是否有关联单据正在进行
		Long id = (Long)paraMap.get("id");
		StringBufferProxy sql = new StringBufferProxy();
		String published = "2", running = "3";
		sql.appendSingle("milepostid = {0} and taskstate in ('{1}','{2}')", id, published, running);
		long taskCount = pmTaskService.getCount(sql.toString());
		if(taskCount>0){
			return ResponseResult.error("该里程碑有未完成的任务，不允许关闭。");
		}
		PmMilepostEntity pmMilepostEntity = this.service.queryById(id.toString());
		pmMilepostEntity.setIsfinished("1");
		pmMilepostEntity.setActualfinishtime(new Date());
		this.service.updateEntity(pmMilepostEntity);
		return ResponseResult.ok(pmMilepostEntity);
	}

	@Override
	@GetMapping({"/list"})
	public ResponseResult<Object> list(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			Query query = new Query(paraMap);
			String sqlWhere = this.spellListSql(paraMap);
			paraMap.put("whereSql",sqlWhere);
			int totalCount = this.service.getCount(paraMap,"getPmMilepostAndProjectCount");
			List<Map<String, Object>> tempList = null;
			HashMap<String, Object> paraMap2 = new HashMap<>();
			paraMap2.put("curPageIndex",query.getCurrentPage());
			int beginCount=query.getCurrentPage()*query.getPageSize()-query.getPageSize();
			paraMap2.put("begincount",beginCount);
			paraMap2.put("pagesize", query.getPageSize());
			paraMap2.put("sidx", query.getSidx());
			paraMap2.put("sord", query.getSord());
			paraMap2.put("whereSql", sqlWhere);
			if (query.getPageSize() > 0) {
				tempList = this.service.queryMapForPage(paraMap2,"getPmMilepostAndProject");
			} else {
				tempList = this.service.queryMap(paraMap2,"queryMapByWhere1");
			}
			this.service.setDispFields(tempList);
			Page page = new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
			return ResponseResult.ok(page);
		} catch (Exception var7) {
			return this.handleControllerException(var7);
		}
	}


}

